/* 
 * File:   Buffer.cpp
 * Author: max
 * 
 * Created on May 23, 2013, 9:49 PM
 */

#include <string.h>

#include "common_Buffer.h"

using namespace TP;

Buffer::Buffer(const uint32_t &pSize){

    if (pSize > 0)
    {
        _c_buffer = new uint8_t [pSize];
        _size = pSize;
        this->Zero();
    }
    else
    {
        _c_buffer = NULL;
        _size = 0;
    }
}

Buffer::~Buffer(){

    if (NULL != _c_buffer)
    {
        delete [] _c_buffer;
    }

    _c_buffer = NULL;
    _size = 0;
}

size_t Buffer::Write (const uint8_t* pData, const size_t &pBytesQuantity) throw (Excepcion&) {
    
    if (pBytesQuantity > _size)
    {
        throw Excepcion ("La cantida de bytes a escribir en el buffer excede la longitud permitida.");
    }
    
    bzero (_c_buffer, _size);
    bcopy(pData, _c_buffer, pBytesQuantity);
    
    return pBytesQuantity;
}

uint8_t* Buffer::c_buffer (){
    return _c_buffer;
}

size_t Buffer::Size(){
    return _size;
}

void Buffer::Zero(){
    
    bzero(_c_buffer, _size);
}